1691B - Shoe Shuffling - CodeForces Solution


constructive algorithms greedy implementation two pointers *1000

Please click on ads to support us..

Python Code:

def rotate(temp_ls):
	a = temp_ls.pop(0)
	temp_ls.append(a)
	return temp_ls

def print_func(temp_ls):
	for i in temp_ls:
		print(i, end = " ")
	print("")

for _ in range(int(input())):
	n = int(input())
	s = list(map(int,input().split()))
	ls=[]
	x=1
	out = []
	temp =[]

	for i in range(len(s)):
		if i == n-1:
			if s[i] == s[i-1]:
				temp.append(i+1)
			else:
				temp = [i+1]

			if len(temp) == 1:
				print("-1")
				x = 0
				break
				
			for j in rotate(temp):
				out.append(j)
		elif i == 0:
			temp =[i+1]
		else:
			if s[i] == s[i-1]:
				temp.append(i+1)
			else:
				if len(temp) == 1:
					print("-1")
					x = 0
					break
				
				for j in rotate(temp):
					out.append(j)
				temp = [i+1]

	if x:print_func(out)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
 ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin>>t;

while(t--)
{
ll n;
cin>>n;
ll a[n];
set<int> s;
map<int,int> m;
for(int i=0;i<n;i++){cin>>a[i]; m[(a[i])]++;}

 
int f=0;
for(auto x:m)
{
    if(x.second==1)
    {
        cout<<"-1"<<endl;
        f=1;
        break;
    }
}
if(!f)
{
    vector<int> v(n+1);
    for(int i=1;i<=n;i++)
    {
  v[i]=i;
    }
    for(int i=1;i<n;i++)
    {
        if(a[i-1]==a[i])
        {
            swap(v[i],v[i+1]);
        }
    }
    for(auto x:v)
    {
        if(x==0){continue;}
        cout<<x<<" ";
    }
    cout<<endl;  
}



}
 return 0;
}


Comments

Submit
0 Comments
More Questions

230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns